package com.qm.console.service.notice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qm.console.component.util.HttpInvokeUtil;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.pubdef.constant.UrlConstant;
import com.qm.console.pubdef.dto.notice.NoticeUpdateAdminDTO;
import com.qm.console.pubdef.dto.notice.RecommendUpdateAdminDTO;
import com.qm.console.service.notice.NoticeService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {
    private final String noticeAdminUrl="/notice";

    @Override
    public BaseRsp listNoticeAdmin(Map<String, String> paramMap) {
        String url= UrlConstant.ApiServerHost+noticeAdminUrl+"/list";
        String body= HttpInvokeUtil.httpGet(url,paramMap, null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        Object rspData=rsp.getData();
        final JSONObject data=(JSONObject) rspData;
        if(data.containsKey("alertNotice")){
            JSONObject list= data.getJSONObject("alertNotice");
            if(list!=null&&list.containsKey("id")){
                String id=list.getString("id");
                list.put("id",id);
            }
        }
        if(data.containsKey("bannerNoticeList")){
            JSONArray list= data.getJSONArray("bannerNoticeList");
            if(list!=null){
                for(int i=0;i<list.size();i++){
                    JSONObject item=list.getJSONObject(i);
                    String id=item.getString("id");
                    if(id!=null){
                        item.put("id",id);
                    }
                }
            }
        }
        return rsp;
    }

    @Override
    public  BaseRsp updateNoticeAdmin(NoticeUpdateAdminDTO updateDTO){
        String url= UrlConstant.ApiServerHost+noticeAdminUrl;
        String req= JSONObject.toJSONString(updateDTO);
        String body=HttpInvokeUtil.httpPostJson(url,req,UrlConstant.DefaultEncode,null);
        BaseRsp rsp=JSONObject.parseObject(body,BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp deleteNoticeAdmin(String noticeId){
        JSONObject req=JSONObject.parseObject(noticeId);
        String url= UrlConstant.ApiServerHost+noticeAdminUrl+"/"+req.get("noticeId");
        String body=HttpInvokeUtil.httpDelete(url,null);
        BaseRsp rsp=JSONObject.parseObject(body,BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp listRecommendAdmin(Map<String, String> paramMap) {
        String url= UrlConstant.ApiServerHost+noticeAdminUrl+"/recommend/list";
        String body= HttpInvokeUtil.httpGet(url,paramMap, null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp addRecommendAdmin(RecommendUpdateAdminDTO updateDTO) {
        String req=JSONObject.toJSONString(updateDTO);
        String url= UrlConstant.ApiServerHost+noticeAdminUrl+"/recommend/add";
        String body=HttpInvokeUtil.httpPostJson(url,req,UrlConstant.DefaultEncode,null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp deleteRecommendAdmin(String userId){
        JSONObject req=JSONObject.parseObject(userId);
        String url= UrlConstant.ApiServerHost+noticeAdminUrl+"/recommend/"+req.get("userId");
        String body=HttpInvokeUtil.httpDelete(url,null);
        BaseRsp rsp=JSONObject.parseObject(body,BaseRsp.class);
        return rsp;
    }
}
